package com.heyqing.topic.LongestCommonPrefix_0014;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * ClassName:LongestCommonPrefix
 * Package:com.heyqing.topic.LongestCommonPrefix_0014
 * Description:
 *
 * @Date:2024/9/9
 * @Author:Heyqing
 */
public class LongestCommonPrefix {

    /**
     * 最长公共前缀
     *
     * @param strs
     * @return
     */
    public String longestCommonPrefix(String[] strs) {
        StringBuffer result = new StringBuffer();
        String for1 = strs[0];
        int sizeFor1 = strs[0].length();
        for (int i = 0; i < sizeFor1; i++) {
            for (int j = 1; j < strs.length; j++) {
                System.out.println(strs[j].length());
                System.out.println(i);
                if (strs[j].length() <= i || for1.charAt(i) != strs[j].charAt(i) ) {
                    return result.toString();
                }
            }
            result.append(for1.charAt(i));
        }
        return result.toString();
    }

    public static void start() {
        LongestCommonPrefix longestCommonPrefix = new LongestCommonPrefix();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入数组长度：");
        int length = sc.nextInt();
        sc.nextLine();
        String[] strs = new String[length];
        for (int i = 0; i < length; i++) {
            System.out.print("第" + (i + 1) + "个：");
            String s = sc.nextLine();
            strs[i] = s;
        }
        String s = longestCommonPrefix.longestCommonPrefix(strs);
        System.out.println("最长公共前缀:" + s);
    }
}
